﻿-- Author      : Kurapica
-- Create Date : 8/04/2008 00:06
     
-- ScrollBar
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.ScrollBar", version) then
		return
	end
    
    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
	local _WidgetName = "ScrollBar"
    local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _Enum = IGAS.GUI.Base.Enum
	local _PropertyType = IGAS.GUI.Base.PropertyType
	
    -- ScrollBar Template
    local TEMPLATE_NONE = "NONE"
    local TEMPLATE_LITBD = "LightBorder"
    
    local _frmSingle
    
    -- Define Block
    _Enum["ScrollBarStyle"] = {
        TEMPLATE_NONE,
        TEMPLATE_LITBD,
    }
    
    _PropertyType["ScrollBarStyle"] = {
        ["Type"] = "enum",
		["EnumType"] = _Enum.ScrollBarStyle,
    }
    
	-- Scripts
    local function OnValueChanged(self, value)
		if self.Parent.SetVerticalScroll then
			self:GetParent():SetVerticalScroll(value)
		end
    end
    
    local function clickUpBtn(self)
        local slider = self:GetParent():GetChild("Slider")
        
		slider:SetValue(slider:GetValue() - slider:GetValueStep());
        PlaySound("UChatScrollButton")
    end
    
    local function clickDownBtn(self)
        local slider = self:GetParent():GetChild("Slider")
        
        slider:SetValue(slider:GetValue() + slider:GetValueStep());
        PlaySound("UChatScrollButton")
    end
    
    local function OnMouseWheel(self,value)
        local iMin, iMax = self:GetMinMaxValues()

        if value > 0 then
            if IsShiftKeyDown() then
                self.Value = iMin            
            else
                 if self.Value - self.ValueStep > iMin then
                   self.Value = self.Value - self.ValueStep
                else
                   self.Value = iMin
                end
            end
        elseif value < 0 then
            if IsShiftKeyDown() then
                self.Value = iMax           
            else
                if self.Value + self.ValueStep < iMax then
                   self.Value = self.Value + self.ValueStep
                else
                   self.Value = iMax
                end
            end
        end
    end
         
    local function scrollChg(self)
        local iMin, iMax = self:GetMinMaxValues()
                
        if self:GetValue() == iMin then
            btnUp = self:GetParent():GetChild("UpBtn"):Disable()
        else
            btnUp = self:GetParent():GetChild("UpBtn"):Enable()
        end
        
        if self:GetValue() == iMax then
            self:GetParent():GetChild("DownBtn"):Disable()
        else
            self:GetParent():GetChild("DownBtn"):Enable()
        end
        
        self:GetParent():Fire("OnValueChanged", self:GetValue())
    end
    
	local _FrameBackdrop = {
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        tile = true, tileSize = 16, edgeSize = 12,
        insets = { left = 0, right = 0, top = 5, bottom = 5 }
	}
    
	-- ScriptType
	local _ScriptType = {
        ["OnValueChanged"] = true,
    }    
    	
	-- FuncProxy
	local _FuncProxy = {
        
        -- Used for ButtonTemplate
        ["SetStyle"] = function(self, style)
            local chk = false
            local t
            
            -- Check Style
            if not style or type(style) ~= "string" then
                return
            end
            
            for _, v in ipairs(_Enum["ScrollBarStyle"]) do
                if v == style then
                    chk = true
                    break
                end
            end
            
            if (not chk) or style == self.__Style then
                return
            end
            
            -- Change Style
            if style == TEMPLATE_NONE then
               frame:SetBackdrop(nil)
            elseif style == TEMPLATE_LITBD then
               frame:SetBackdrop(_FrameBackdrop)
            end
        end,
        
        ["GetStyle"] = function(self)
            return self.__Style or TEMPLATE_NONE
        end,
        
        -- GetMinMaxValues() - Get the current bounds of the slider. 
        ["GetMinMaxValues"] = function(self)
            return self:GetChild("Slider"):GetMinMaxValues()
        end,
        
        -- GetValue() - Get the current value of the slider. 
        ["GetValue"] = function(self)
            return self:GetChild("Slider"):GetValue()
        end,
        
        -- GetValueStep() - Get the current step size of the slider.  
        ["GetValueStep"] = function(self)
            return self:GetChild("Slider"):GetValueStep()
        end,
        
        -- SetMinMaxValues(iMin, iMax) - Set the bounds of the slider.  
        ["SetMinMaxValues"] = function(self, iMin, iMax)
            self:GetChild("Slider"):SetMinMaxValues(iMin, iMax)
        end,
        
        -- SetValue(value) - Set the value of the slider. Also causes the thumb to show on the first call. 
        ["SetValue"] = function(self, value)
            self:GetChild("Slider"):SetValue(value)
        end,
        
        -- SetValueStep(value) - Set the step size of the slider. 
        ["SetValueStep"] = function(self, value)
            self:GetChild("Slider"):SetValueStep(value)
        end,
        
        -- Disable
        ["Disable"] = function(self)
            self:GetChild("DownBtn"):Disable()
            self:GetChild("UpBtn"):Disable()
            self:GetChild("Slider"):GetThumbTexture():Hide()
        end,
        
        -- Enable
        ["Enable"] = function(self)            
            local iMin, iMax = self:GetMinMaxValues()
            
            self:GetChild("Slider"):GetThumbTexture():Show()
                    
            if self:GetValue() == iMin then
                btnUp = self:GetChild("UpBtn"):Disable()
            else
                btnUp = self:GetChild("UpBtn"):Enable()
            end
            
            if self:GetValue() == iMax then
                self:GetChild("DownBtn"):Disable()
            else
                self:GetChild("DownBtn"):Enable()
            end
        end,
        
        -- IsEnabled
        ["IsEnabled"] = function(self)
            return self:GetChild("Slider"):GetThumbTexture():IsShown()
        end,
    }
    
	-- Property
	local _Property = {
        -- MinMaxValue
        ["MinMaxValue"] = {
			["Set"] = function(self, set)
				self:SetMinMaxValues(set.min, set.max)
			end,
			
			["Get"] = function(self)
				local set = {}
				set.min, set.max = self:GetMinMaxValues()
				return set
			end,
			
			["Type"] = _PropertyType.MinMaxValue,
		},
	
        -- Value
        ["Value"] = {
			["Set"] = function(self, value)
				self:SetValue(value)
			end,
			
			["Get"] = function(self)				
				return self:GetValue()
			end,
			
			["Type"] = _PropertyType.Number,
		},
    
        -- ValueStep
        ["ValueStep"] = {
			["Set"] = function(self, value)
				self:SetValueStep(value)
			end,
			
			["Get"] = function(self)				
				return self:GetValueStep()
			end,
			
			["Type"] = _PropertyType.Number,
		},
        
        -- Enabled
        ["Enabled"] = {
			["Set"] = function(self, flag)
                if flag then
                    self:Enable()
                else
                    self:Disable()
                end
			end,
			
			["Get"] = function(self)
				return (self:IsEnabled() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
        },
    }
    
    -- New    
    local _New = function(parent)
        -- ScrollBar
		local scrollBar = CreateFrame("Frame",nil,parent)
		
        scrollBar:SetWidth(18)
        scrollBar:ClearAllPoints()
        scrollBar:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -4, -4)
        scrollBar:SetPoint("BOTTOM", parent, "BOTTOM", 0, 4)        

        local slider = CreateFrame("Slider", "Slider", scrollBar)
        slider:ClearAllPoints()
        slider:SetPoint("LEFT", scrollBar, "LEFT", 0, 0)
        slider:SetPoint("RIGHT", scrollBar, "RIGHT", 0, 0)
        slider:SetPoint("TOP", scrollBar, "TOP", 0, -17)
        slider:SetPoint("BOTTOM", scrollBar, "BOTTOM", 0, 17)
        slider:SetThumbTexture("Interface\\Buttons\\UI-ScrollBar-Knob.blp")
        slider.MouseWheelEnabled = true
        
        local texture = slider:GetThumbTexture()
        texture:SetTexCoord(7/32, 25/32, 7/32, 24/32)
        texture:SetWidth(18)
        texture:SetHeight(17)
        slider:SetMinMaxValues(1, 10)
        slider:SetValueStep(1)
        slider:SetValue(1)

        local btnScrollUp = CreateFrame("Button", "UpBtn", scrollBar)
        btnScrollUp:SetWidth(32)
        btnScrollUp:SetHeight(32)
        btnScrollUp:ClearAllPoints()
        btnScrollUp:SetPoint("TOP", scrollBar, "TOP", 0, 7)
        btnScrollUp:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Up.blp")
        btnScrollUp:SetPushedTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Down.blp")
        btnScrollUp:SetDisabledTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Disabled.blp")
        btnScrollUp:SetHighlightTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Highlight.blp", "ADD")
        btnScrollUp:SetHitRectInsets(6, 7, 7, 8)

        local btnScrollDown = CreateFrame("Button", "DownBtn", scrollBar)
        btnScrollDown:SetWidth(32)
        btnScrollDown:SetHeight(32)
        btnScrollDown:ClearAllPoints()
        btnScrollDown:SetPoint("BOTTOM", scrollBar, "BOTTOM", 0, -8)
        btnScrollDown:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Up.blp")
        btnScrollDown:SetPushedTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Down.blp")
        btnScrollDown:SetDisabledTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Disabled.blp")
        btnScrollDown:SetHighlightTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Highlight.blp", "ADD")
        btnScrollDown:SetHitRectInsets(6, 7, 7, 8)
        
        -- Event Handle
        btnScrollUp.OnClick = clickUpBtn
        btnScrollDown.OnClick = clickDownBtn
        slider.OnValueChanged = scrollChg
        slider.OnMouseWheel = OnMouseWheel
        
        -- Can be override, Special use.
        scrollBar.__StackScripts["OnValueChanged"] = {
			[0] = OnValueChanged,
		}
        
        btnScrollUp:Disable()
        btnScrollDown:Disable()
        
        return scrollBar
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)    
end